<template>
  <div class="app-container" style="margin-top: -25px">
<!--
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="itemId">
        <el-input
          v-model="queryParams.itemId"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
-->

 <!--   <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:caigouDetail:add']"
        >选择物料</el-button>
      </el-col>
     &lt;!&ndash; <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:rukudetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>&ndash;&gt;
    </el-row>-->
<!--    <button v-print="printObj">打印</button>-->
    <div ref="tableRef" id="main" style="width: 88%">
      <div style="width: 100%;text-align: center;font-size: 24px;margin-bottom:20px;">泰康锻造股份有限公司原材料入库单</div>
<!--      <div style="width: 100%;text-align: center;font-size: 20px;margin-bottom:10px;">入库单</div>-->
      <el-form :model="form2" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="86px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="供货单位" prop="supplyName">
              {{rowData.supplyName}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据编号" prop="danhao">
              {{rowData.danhao}}
            </el-form-item>
          </el-col>
         <!-- <el-col :span="8">
            <el-form-item label="支付方式" prop="szFs">
              {{rowData.szFs}}
            </el-form-item>
          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="供货日期" prop="ghDate">
              {{rowData.ghDate}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供货车号" prop="ghCarNum">
              {{rowData.ghCarNum}}
            </el-form-item>
          </el-col>
        </el-row>
       <!-- <el-row>
          <el-col :span="8">
            <el-form-item label="银行账户" prop="zhNum">
              {{rowData.zhNum}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="票号" prop="piaoNum">
              {{rowData.piaoNum}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收票日期" prop="piaoDate">
              {{rowData.piaoDate}}
            </el-form-item>
          </el-col>
        </el-row>-->
      </el-form>
      <el-table style="width: 100%;border-bottom: 1px solid black"  v-loading="loading" :data="rukudetailList" @selection-change="handleSelectionChange" row-key="columnId" :max-height="tableHeight">
        <el-table-column label="产品名称" width="150" align="center" prop="itemName" />
        <el-table-column label="规格型号"  width="150" align="center" prop="spec" />
        <el-table-column label="入库单位" align="center" prop="unit">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.jc_iteminfo_unit" :value="scope.row.unit"/>
          </template>
        </el-table-column>
<!--        <el-table-column label="供应商名称" align="center" prop="supplyName" />-->
        <el-table-column label="批次" align="center" prop="lot" />
        <el-table-column label="物料类型" align="center" prop="className" />
        <el-table-column label="入库数量" align="center" prop="rukuNum"/>
        <el-table-column label="单价" align="center" prop="unitPrice"/>
        <el-table-column label="金额" align="center" prop="totalPrice"/>
      </el-table>
      <el-form :model="form2" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="86px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="付款" prop="beginTime">
              {{rowData.totalPrice}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制单人" prop="createBy">
              {{rowData.createBy}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总数量" prop="zsl">
              {{zsl}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="print-button-container">
      <el-button type="primary" icon="el-icon-printer"  @click="billPrintClick">打印</el-button>
    </div>
    <!--选择物料-->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" style="margin-top: -20px"  :model="form" :rules="rules" label-width="80px">
        <IteminfoSelect ref="iteminfoSelect"></IteminfoSelect>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">选 择</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--<pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->
  </div>
</template>

<script>
import { listRukudetail, getRukudetail,addRukuItemInfo, delRukudetail, addRukudetail, updateRukudetail } from "@/api/business/rukudetail";
import IteminfoSelect from '@/views/base/iteminfo/iteminfoSelect';

export default {
  components: {
    IteminfoSelect
  },
  name: "RukuSelect",
  dicts: ['jc_iteminfo_unit'],
  props:{
      rowData:{
            type:Object,
      }
  },
  data() {
    return {
      // printObj: {
      //   id: "main",    // 这里是要打印元素的ID
      //   popTitle: '',  // 打印的标题
      //   extraCss: '',  // 打印可引入外部的一个 css 文件
      //   extraHead: ''  // 打印头部文字
      // },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      itemIds: [],
      zsl:0,
      zjg:0,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库详情表格数据
      rukudetailList: [],
      // 表格的高度
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainId: null,
        rukuNum: null,
        rukuDate: null,
        itemName: null,
        itemId: null,
        supplyId: null,
        supplyName: null,
        stockId: null,
        stockName: null,
        price: null,
        classId: null,
        className: null,
        danhao:null,
      },
      // 表单参数
      form: {},
      form2:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
   /* this.getList();*/
  },
  filters: {
    numFilter (value) {
      // 截取当前数据到小数点后两位
      let realVal = parseFloat(value).toFixed(2)
      return realVal
    }
  },
  methods: {
    billPrintClick() {
      let newstr = this.$refs.tableRef.innerHTML  // 获取需要打印的内容
      let oldstr = document.body.innerHTML  // 获取别打印页面的内容
      document.body.innerHTML = newstr
      // 获取表格表头，表格内容改变打印内容字体的颜色
      document.getElementsByTagName('thead')[0].style.color = '#000'
      document.getElementsByTagName('tbody')[0].style.color = '#000'
      window.print()
      document.body.innerHTML = oldstr
      window.location.reload()  // 退出刷新页面
      return false
    },
    /** 查询入库详情列表 */
    getList() {
      this.loading = true;
      listRukudetail(this.queryParams).then(response => {
        this.rukudetailList = response.rows;
        this.zsl = 0
        for(let i=0;i<this.rukudetailList.length;i++){
          this.zsl += this.rukudetailList[i].rukuNum
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    getRkSelectList(danhao){
      this.queryParams.danhao=danhao;
      this.getList()
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        if (index === 1 || index === 2 || index === 3 || index === 4  || index === 6)  {
          return '';
        }
        /*for(var i=0 ;i<data.length;i++){
          data[i].totalPrice =data[i].caigouNum*data[i].unitPrice;
        }*/
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              if(index==5){
                return prev + curr
              }else{
                return this.keepTwoDecimalFull(Number(prev) + curr);
              }
            } else {
              return prev;
            }
          }, 0);
          sums[index];
        } else {
          sums[index] = '';
        }
      });

      return sums;
    },
    keepTwoDecimalFull(num) {
      var result = parseFloat(num);
      if (isNaN(result)) {
        alert('传递参数错误，请检查！');
        return false;
      }
      result = Math.round(num * 100) / 100;
      var s_x = result.toString(); //将数字转换为字符串

      var pos_decimal = s_x.indexOf('.'); //小数点的索引值


      // 当整数时，pos_decimal=-1 自动补0
      if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
      }

      // 当数字的长度< 小数点索引+2时，补0
      while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
      }
      return s_x;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mainId: null,
        rukuNum: null,
        rukuDate: null,
        itemName: null,
        itemId: null,
        supplyId: null,
        supplyName: null,
        stockId: null,
        stockName: null,
        price: null,
        classId: null,
        className: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /*重置列表*/
    listReset(){
      this.rukudetailList =[];
      this.loading = false;
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "选择物料信息";
      this.itemIds=[];
      if(this.rukudetailList.length>0){
        this.rukudetailList.map((item) => {
          this.itemIds.push(item.itemId);
        })
      }
      setTimeout(() => {
        this.$refs.iteminfoSelect.getItemInfoList(this.itemIds);
      }, 5);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRukudetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入库详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const id =this.$refs.iteminfoSelect.ids;
      addRukuItemInfo(id).then(response => {
        response.data.map((item)=>{
          this.rukudetailList.push(item)
        })
      });
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除入库详情编号为"' + ids + '"的数据项？').then(function() {
        return delRukudetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/rukudetail/export', {
        ...this.queryParams
      }, `rukudetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-input-number.is-controls-right .el-input-number__increase{
    display: none;
    visibility: hidden;
    width: 10px !important;
    position: fixed;
    top:0;
  }
  ::v-deep .el-input-number.is-controls-right .el-input-number__decrease{
    display: none;
    visibility: hidden;
    position: fixed;
    top:0;
  }
  ::v-deep .el-table .cell{
    /*display: none;*/
    padding-right: 0;
    padding-left: -30px;
  }
  @media print {
    @page {
      // 纵向
      size: portrait;

      // 横向
      /*size: landscape;*/

      // 边距
      margin: 5cm 0cm 0cm 0cm;
    }
  }
  @media print {
    @page {
      margin: 0;
    }
    body {
      margin: 1cm;
    }
  }

</style>
<style media="print">
  @page {
    size: landscape;
    margin: 3mm;
  }
  @media print {
    /*html {*/
    /*  background-color: #ffffff;*/
    /*  height: auto;*/
    /*  margin: 0px;*/
    /*}*/
    /*body {*/
    /*  border: solid 1px #ffffff;*/
    /*  background:red;*/
    /*}*/
    #main{
      font-size: 40px;
    }
    #main table {

      table-layout: auto !important;
    }
    #main .el-table__header-wrapper .el-table__header {
      width: 100% !important;
      border: solid 1px #f2f2f2;
    }
    #main .el-table__body-wrapper .el-table__body {
      width: 100% !important;
    }
    #main #pagetable table {
      table-layout: fixed !important;
    }
  }

</style>
